<?php
namespace api\know\validate;

use api\know\model\StudentModel;
use think\Validate;
use think\facade\Log;

class StudentValidate extends Validate
{
    protected $rule = [
        'age' => 'require',
		'name' => 'require',
        'sex' => 'require',
        'parent_name' => 'require',
        'parent_mobile' => 'require',
        'id' => 'require|gt:0',
    ];

    protected $message = [
        'age.require' => '年龄为必填项',
        'name.require' => '姓名为必填项',		
        'sex.require' => '性别为必填项',
        'parent_name.require' => '父母姓名为必填项',
        'parent_mobile.require' => '父母联系方式为必填项',
        'id.require' => 'id不能为空',
    ];

    protected $scene = [
        'add'  => ['name', 'sex', 'age', 'parent_name', 'parent_mobile'],
        'edit'  => ['id', 'name', 'sex', 'age', 'parent_name', 'parent_mobile'],
        'delete' => ['id'],
    ];

    protected function checkContentRepeat($value, $rule, $data)
    {
        if ($this->currentScene != 'edit') 
        {
            return true;
        }

        $model = new NewWordModel();
        $item  = $model->where('chapter_uuid', $data['chapter_uuid'])
                        ->where('id', '<>', $data['id'])
                        ->where('content', $data['content'])
                        ->find();

        if (empty($item)) 
        {
            return true;
        } 
        else 
        {
            return $data['content'] . "---已经存在---";
        }
    }

    protected function checkName($value, $rule, $data)
    {
        if ($this->currentScene != 'add') 
        {
            return true;
        }

        $model = new NewWordModel();
        $item  = $model->where('chapter_uuid', $data['chapter_uuid'])
					   ->where('content', $data['content'])
                       ->find();

        if (empty($item)) 
        {
            return true;
        } 
        else 
        {
            return $data['content'] . "---已经存在---";
        }
    }
}